﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Eirene.Core.Model;

namespace Eirene.WPF.ViewModel
{
    /// <summary>
    /// Presents a <see cref="Item"/> instance.
    /// </summary>
    /// <remarks>
    /// Using an aggregation; expose-as-needed approach.
    /// </remarks>
    public class ItemViewModel
    {
        /// <summary>
        /// Initalizes a new instance of the ItemViewModel class.
        /// </summary>
        public ItemViewModel(Item data)
        {
            Contract.Requires(data != null);
            Data = data;
        }

        /// <summary>
        /// Gets the aggregated data source, allowing view to direct bind where the view-model doesn't 
        /// provide a better alternative.
        /// </summary>
        public Item Data { get; private set; }

        /// <summary>
        /// Access to the internal objects <see cref="Item.Owners"/> as view-model objects.
        /// </summary>
        /// <remarks>
        /// To promote a model instance into a view-model instance,
        /// am using a pattern of re-presenting collections, and instance aggregation.
        /// </remarks>
        public IEnumerable<OwnerViewModel> Owners
        {
            get
            {
                foreach (var next in this.Data.Owners)
                {
                    yield return new OwnerViewModel(next);
                }
            }
        }

        /// <summary>
        /// Re-present the child items collection as child view-model items.
        /// </summary>
        public IEnumerable<ItemViewModel> Children
        {
            get
            {
                foreach (var next in this.Data.Children)
                {
                    yield return new ItemViewModel(next);
                }
            }
        }
    }
}
